<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>11.手写bind</title>
</head>
<body>

  <script>
    // 原生bind
    /* function fn1(a,b,c) {
      console.log('this', this);
      console.log(a,b,c);
      return 'this is fn1'
    }

    const fn2 = fn1.bind({x: 100}, 1,2,3)
    const res = fn2()
    console.log(res); */


    Function.prototype.mybind = function() {
      // 1. 将参数变为数组
      let args = Array.prototype.slice.call(arguments)
      // 获取this
      let that = args.shift()
      // 获取函数 fn1.bind() 中的fn1
      let self = this

      return function() {
        return self.apply(that, args)
      }
    }

    function fn1(a,b,c) {
      console.log('this', this);
      console.log(a,b,c);
      return 'this is fn1'
    }

    const fn2 = fn1.mybind({x: 300}, 1,2,3)
    const res = fn2()
    console.log(res);


  </script>
  
</body>
</html>